<?php
require_once(DIR_LIBRARY."orm".DS."orm.php");
/**
 * this is common thread post framework for thread and post application
 *
 */
class CtpPost extends ORM
{
    const STATUS_AVAILABLE = 1;
    const STATUS_DELETED   = 2;

    private static $instance;

    protected $dsn       = "default";
    protected $tablename = "post";
    protected $rebuildTableName = true;
    protected $validFields = array("id", "parent", "sequence", "body", "meta", "status", "created", "updated", "creator");

    public function __construct($id = NULL)
    {
        parent::__construct($id);
    }

    //get threads first post
	public function getPosts($threads)
	{
        $rows = array();
        if (!$threads)
        {
            return $rows;
        }

        $thread_ids = array();
        foreach ($threads as $v)
        {
            $thread_ids[] = $v["id"];
        }

        $count = count($thread_ids);
		if ($count > 1)
		{
			$this->appendWhere("parent IN(".implode(",", $thread_ids).')');
		}
		else if (1 == $count)
		{
			$this->appendWhere('parent = "'.$thread_ids[0].'"');
		}

		$this->appendWhere('sequence = "1"');

        //for build table name
        $this->setApp($threads[$thread_ids[0]]["app"]);
        $this->setShard($threads[$thread_ids[0]]["shard"]);

        $rs = $this->execute();

        while ($row = $rs->fetchRow())
        {
            $rows[$row["parent"]] = $row;
        }

        return $rows;
	}

    //get thread first post 
	public function getPost($thread, $sequence = 1)
	{
		$thread_id = $thread->getId();
        $this->setApp($thread->getApp());
        $this->setShard($thread->getShard());
        $this->setParent($thread_id);
		$this->setSequence($sequence);
		$this->load();

        return $this;
	}

	public function getListBySequence($thread, $sequence = 1, $page_size = parent::DEFAULT_PAGE_SIZE)
	{
        $thread_id = $thread->getId();
		if (!$thread_id)
		{
			throw new Exception("no thread id");
		}

		$params = array("thread_id" => $thread_id);

		return self::getListBySequence($sequence, $page_size, $params);	
	}

    public function create($thread)
    {
        //check  first
        $body     = $this->getBody();
        $creator  = $this->getCreator();

        if (!$creator)
        {
            throw new Exception("no creator");
        }

        if (!$body)
        {
            throw new Exception("no body");
        }

        $time = time();

        $orm = self::factory();
        $app = $thread->getApp();
        $shard = $thread->getShard();
        $parent = $thread->getId();
        $orm->setApp($app);
        $orm->setShard($shard);
        $orm->setParent($parent);
        $sequence = $orm->getMax("sequence") + 1;
        unset($orm);

        $this->setApp($app);
        $this->setShard($shard);
        $this->setSequence($sequence);
		$this->setParent($parent);

        $this->setStatus(self::STATUS_AVAILABLE);
        $this->setCreated($time);
        $this->setUpdated($time);

        //create
        return parent::create();
    }

    public function update($thread, $sequence = 0)
    {
        $time = time();
        $this->setApp($thread->getApp());
        $this->setParent($thread->getId());
        $this->setShard($thread->getShard());
        $this->appendWhere("parent=".$thread->getId());

        if ($sequence)
        {
            $this->appendWhere("sequence=".$sequence);
        }

        $this->setUpdated($time);
        
        //update
        return parent::update();
    }

    public function delete($thread, $sequence = 0)
    {
        $this->setApp($thread->getApp());
        $this->setShard($thread->getShard());
        $this->setParent($thread->getId());
        $this->appendWhere("parent=".$thread->getId());

        $this->setStatus(self::STATUS_DELETED);

        if ($sequence)
        {
            $this->appendWhere("sequence=".$sequence);
        }


        return parent::update();
    }

    public function rebuildTableName()
    {
		return $this->buildTableName();
    }

    private function buildTableName()
    {
		return $this->tablename."_".$this->getApp()."_".$this->getShard();
    }

    public static function factory($id = NULL)
    {
        return new self($id);
    }

    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self();
        }

        return self::$instance;
    }
    
} //end class
